package org.sanmao.wyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.sanmao.wyj.controller.utils.Result;
import org.sanmao.wyj.dao.WorkerDao;
import org.sanmao.wyj.domain.Cashout;
import org.sanmao.wyj.domain.User;
import org.sanmao.wyj.domain.Worker;
import org.sanmao.wyj.exception.AccountIsExistException;
import org.sanmao.wyj.service.CashoutService;
import org.sanmao.wyj.service.WorkerService;
import org.sanmao.wyj.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class WorkerServiceImpl extends ServiceImpl<WorkerDao,Worker> implements WorkerService {

    @Autowired
    private WorkerDao workerDao;
    @Autowired
    private CashoutService cashoutService;

    @Override
    public IPage<Worker> findWorkersPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        workerDao.selectPage(page, null);
        return page;
    }

    @Override
    public String login(Worker worker) {
        Worker isworker = workerDao.findWorkerByUsernameAndPassword(worker.getUsername(),worker.getPassword());
        if(isworker != null)
        {
            int id = isworker.getWid();
            String un = isworker.getUsername();
            String pw = isworker.getPassword();
            Map<String, Object> workerInfo = new HashMap<>();
            workerInfo.put("username", un);
            workerInfo.put("password", pw);
            String token = JwtUtils.sign(id, workerInfo);
            return  token;
        }
        else return null;
    }

    @Override
    public List<Worker> findAllWorkers() {
        List<Worker> workers = workerDao.selectList(null);
        return workers;
    }

    @Override
    public Worker findWorkerById(int id) {
        Worker worker = workerDao.selectById(id);
        return worker;
    }

    @Override
    public Boolean saveWorker(Worker worker) {
        String username = worker.getUsername();
        Worker existAccount = workerDao.findWorkerByUsername(username);
        try {
            if(existAccount != null){
                throw new AccountIsExistException("账号已存在！");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            return false;
        }
        int insert = workerDao.insert(worker);
        return insert>0;
    }

    @Override
    public Boolean updateWorker(Worker worker) {
        int i = workerDao.updateById(worker);
        return i>0;
    }

    @Override
    public Boolean deleteWorkerById(int id) {
        int i = workerDao.deleteById(id);
        return i>0;
    }

    @Override
    public IPage<Worker> findWorkersByName(String name, int currentPage, int pageSize) {
        IPage<Worker> page = new Page<>(currentPage,pageSize);
        return workerDao.findWorkersByName(page,name);

    }

    @Override
    public Boolean cashOut(Cashout cash, HttpServletRequest request) {
        String token = request.getHeader("token");
        int id = JwtUtils.getUserId(token);
        Worker worker = workerDao.selectById(id);
        double value = cash.getCashoutValue();
        System.out.println(cash);
        double originbalance = worker.getBalance();
        if(originbalance < value)
        {
            return false;
        }
        else {
            worker.setBalance(originbalance-value);
            UpdateWrapper<Worker> wrapper = new UpdateWrapper<>();
            wrapper.eq("wid",id);
            workerDao.update(worker,wrapper);
            Date date = new Date(System.currentTimeMillis());
            cash.setTime(date);
            cash.setState(1);
            cash.setWid(id);
            return cashoutService.save(cash);
        }
    }

    @Override
    public Boolean findPassword(String username, String idcard, String password) {
        Worker worker = workerDao.findWorkersByUsernameAndIdcad(username, idcard);
        if(worker != null){
            UpdateWrapper<Worker> wrapper = new UpdateWrapper<>();
            wrapper.eq("username",username).eq("idcard",idcard);
            worker.setPassword(password);
            return  workerDao.update(worker, wrapper) > 0;
        }
        else return false;
    }

    @Override
    public Worker findMe(HttpServletRequest request) {
        String token = request.getHeader("token");
        int id = JwtUtils.getUserId(token);
        return workerDao.findMe(id);
    }

    @Override
    public Boolean updateWorker(Worker worker, HttpServletRequest request) {
        Worker isWorker = workerDao.findWorkerByUsername(worker.getUsername());
        if(isWorker == null){
            String token = request.getHeader("token");
            int id = JwtUtils.getUserId(token);
            Worker originWorker = workerDao.selectById(id);
            double balance = originWorker.getBalance();
            worker.setBalance(balance);
            worker.setWid(id);
            return workerDao.updateById(worker) > 0;
        }
        else return false;
    }
}
